package net.lgpage.http;

import net.lgpage.util.NetWorkUtils;
import android.content.Context;
import android.os.AsyncTask;

public abstract class ExceptionAsyncTask<Result> extends
		AsyncTask<String, Integer, Result> {
	
	
	public static class HttpErrorCodeException extends Exception{

		public HttpErrorCodeException() {
			super();
		}

		public HttpErrorCodeException(String detailMessage, Throwable throwable) {
			super(detailMessage, throwable);
		}

		public HttpErrorCodeException(String detailMessage) {
			super(detailMessage);
		}

		public HttpErrorCodeException(Throwable throwable) {
			super(throwable);
		}
		
	}
	public static class HttpMsgPauseException extends Exception{

		public HttpMsgPauseException() {
			super();
		}

		public HttpMsgPauseException(String detailMessage, Throwable throwable) {
			super(detailMessage, throwable);
		}

		public HttpMsgPauseException(String detailMessage) {
			super(detailMessage);
		}

		public HttpMsgPauseException(Throwable throwable) {
			super(throwable);
		}

	}
	public static class HttpNotingReturnException extends Exception{

		public HttpNotingReturnException() {
			super();
		}

		public HttpNotingReturnException(String detailMessage, Throwable throwable) {
			super(detailMessage, throwable);
		}

		public HttpNotingReturnException(String detailMessage) {
			super(detailMessage);
		}

		public HttpNotingReturnException(Throwable throwable) {
			super(throwable);
		}

	}
	public static class HttpSessionOutTimeException extends Exception{

		public HttpSessionOutTimeException() {
			super();
		}

		public HttpSessionOutTimeException(String detailMessage, Throwable throwable) {
			super(detailMessage, throwable);
		}

		public HttpSessionOutTimeException(String detailMessage) {
			super(detailMessage);
		}

		public HttpSessionOutTimeException(Throwable throwable) {
			super(throwable);
		}

	}
	public static class NoNetworkException extends Exception{

		public NoNetworkException() {
			super();
		}

		public NoNetworkException(String detailMessage) {
			super(detailMessage);
		}

	}
	
	
	Context context;
	HttpExceptionDealer dealer;
	public ExceptionAsyncTask(Context context) {
		super();
		this.context = context;
		this.dealer = new HttpExceptionToastDealer(context);
	}

	@Override
	public void onCancelled() {
		super.onCancelled();
	}
	
	public ExceptionAsyncTask(Context context, HttpExceptionDealer dealer) {
		super();
		this.context = context;
		this.dealer = dealer;
	}

	Exception e;

	public abstract Result doInBackgroundX(String... String)
			throws Exception;

	public abstract void onPostExecuteX(Result result);

	@Override
	public Result doInBackground(String... String) {
		try {
			return doInBackgroundX(String);
		} catch (Exception e) {
			this.e = e;
			e.printStackTrace();
			return null;
		}
	}

	public  void onException(Exception e){
		if(dealer!=null)
			try {
				dealer.deal(e);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
	}
	
	@Override
	public void onPostExecute(Result result) {
		super.onPostExecute(result);
		if (isCancelled()) {
			return;
		}
		if (e != null) {
			onException(e);
			cancel(true);
		} else {
			onPostExecuteX(result);
		}
	}

	public Context getContext() {
		return context;
	}

	public void setContext(Context context) {
		this.context = context;
	}

	@Override
	public void onPreExecute() {
		if(!NetWorkUtils.isConnect(context)){
			cancel(true);
			onException(new NoNetworkException());
		}
	}


}
